import React from 'react';
import {Table, Typography} from 'antd';
import PropTypes from 'prop-types';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {formatAmount, formatDateTime} from '../utils';
const {Text} = Typography;
const {Description} = DescriptionList;

export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };

    render() {
        let totalRewardAmount = 0;
        let totalRewardedSum = 0;
        let totalPrice = 0;
        if(this.props.list) {
            this.props.list.forEach(d => {
                totalRewardedSum += (d.rewardedSum);
                totalRewardAmount += (d.rewardAmount);
            });
            totalPrice = totalRewardedSum + totalRewardAmount;
        }
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.branchCode',
                    defaultMessage: '营销分公司编号'
                }),
                dataIndex: 'branchCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'branchCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.vehicleSalesOrgCode',
                    defaultMessage: '销售组织编号'
                }),
                dataIndex: 'vehicleSalesOrgCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'vehicleSalesOrgCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.dealerCode',
                    defaultMessage: '总代'
                }),
                dataIndex: 'dealerCode',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.rebateNo',
                    defaultMessage: '返利凭证号'
                }),
                dataIndex: 'rebateNo',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'rebateNo' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.date',
                    defaultMessage: '发生日期'
                }),
                dataIndex: 'date',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'date' && this.props.sortedOrder,
                render: text => formatDateTime(text, DATE_FORMAT)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.time',
                    defaultMessage: '发生时间'
                }),
                dataIndex: 'time',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'time' && this.props.sortedOrder,
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.summary',
                    defaultMessage: '文本摘要'
                }),
                dataIndex: 'summary',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.vehicleRebateTypeCode',
                    defaultMessage: '返利类型编号'
                }),
                dataIndex: 'vehicleRebateTypeCode',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.vehicleRebateTypeName',
                    defaultMessage: '返利类型名称'
                }),
                dataIndex: 'vehicleRebateTypeName',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.orderCode',
                    defaultMessage: '发票号'
                }),
                dataIndex: 'orderCode',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.changeType',
                    defaultMessage: '更改类型'
                }),
                dataIndex: 'changeType',
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.rewardAmount',
                    defaultMessage: '应奖励金额'
                }),
                dataIndex: 'rewardAmount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'tablePanel.column.rewardedSum',
                    defaultMessage: '已奖励金额'
                }),
                dataIndex: 'rewardedSum',
                render: text => formatAmount(text)
            }];

        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
                <DescriptionList>
                    <Description term={this.props.intl.formatMessage({
                        id: 'tablePanel.operation.totalRewardAmount',
                        defaultMessage: '应奖励金额汇总'
                    })}>{formatAmount(totalRewardAmount)}
                    </Description>
                    <Description term={this.props.intl.formatMessage({
                        id: 'tablePanel.operation.totalRewardedSum',
                        defaultMessage: '已奖励金额汇总'
                    })}>{formatAmount(totalRewardedSum)}
                    </Description>
                    <Description term={this.props.intl.formatMessage({
                        id: 'tablePanel.operation.totalPrice',
                        defaultMessage: '应奖励已奖励金额汇总'
                    })}>{formatAmount(totalPrice)}
                    </Description>
                </DescriptionList>
            </div>
        );
    }
}

TablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'list', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option)),
    refreshListData: () => dispatch(tableSearch()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
